package com.lagou.edu.request;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author 赵虎
 * @date 2020/11/8 5:48 下午
 */
public class Request {

    private String method;
    private String url;
    private String host;
    private String context;

    public String getMethod() {
        return method;
    }

    public String getUrl() {
        return url;
    }

    public String getHost() {
        return host;
    }

    public String getContext() {
        return context;
    }

    public Request() {
    }

    public Request(InputStream inputStream) throws IOException {
        int count = 0;
        while (count == 0) {
            count = inputStream.available();
        }
        byte[] bytes = new byte[count];
        inputStream.read(bytes);
        String reqText = new String(bytes);
        String[] lineContext = reqText.split("\n");
        String firstLineText = lineContext[0];
        String secondLineText = lineContext[1];
        String[] split = firstLineText.split(" ");
        String[] hostSplit = secondLineText.split(" ");
        this.method = split[0];
        String urlStr = split[1];
        if (urlStr.indexOf("/", 1) != -1) {
            String context = urlStr.substring(1, urlStr.indexOf("/", 1));
            this.url = urlStr.substring(urlStr.indexOf("/", 1));
            this.context = context;
            this.host = hostSplit[1].split(":")[0];
            System.out.println("host:" + host + ",context:" + context + ",路径:" + url);
        }else {
            this.url = urlStr;
            this.host = hostSplit[1].split(":")[0];
        }
    }
}
